﻿using AmazonMODEL;
using MarketplaceWebServiceProducts.Model;
using MODEL;
using Newtonsoft.Json;
using SysController;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SysController
{
    public static class ProductsController
    {
        #region 2015-11-15 获取商品及其属性列表 + static void GetMatchingProduct(AppKey appkey, List<string> ASINList)
        /// <summary>
        /// 2015-11-15 获取商品及其属性列表
        /// </summary>
        /// <param name="appkey"></param>
        /// <param name="ASINList"></param>
        public static void GetMatchingProduct(AppKey appkey, List<string> ASINList)
        {
            GetMatchingProductRequest request = new GetMatchingProductRequest()
            {
                ASINList = new ASINListType(),
                MarketplaceId = appkey.MarketplaceCode,
                SellerId = appkey.MerchantCode
            };
            request.ASINList.ASIN = ASINList;
            try
            {
                GetMatchingProductResponse response = HelperController.RegisterProductsService(appkey).GetMatchingProduct(request);
                if (response.IsSetGetMatchingProductResult())
                {

                    List<GetMatchingProductResult> list = response.GetMatchingProductResult;
                    foreach (var item in list)
                    {
                        AttributeSetList ls = item.Product.AttributeSets;
                        foreach (var it in ls.Any)
                        {

                        }
                    }
                }
            }
            catch (Exception)
            {

                throw;
            }

        } 
        #endregion

        #region  2015-11-15 获取竞争信息 + static List<CompetitiveModel> GetCompetitivePricingForASIN(AppKey appkey, List<string> ASINList)
        /// <summary>
        /// 2015-11-15 获取竞争信息
        /// </summary>
        /// <param name="appkey"></param>
        /// <param name="ASINList"></param>
        /// <returns></returns>
        public static List<CompetitiveModel> GetCompetitivePricingForASIN(AppKey appkey, List<string> ASINList)
        {
            GetCompetitivePricingForASINRequest request = new GetCompetitivePricingForASINRequest()
            {
                ASINList = new ASINListType(),
                MarketplaceId = appkey.MarketplaceCode,
                SellerId = appkey.MerchantCode
            };
            request.ASINList.ASIN = ASINList;
            try
            {
                GetCompetitivePricingForASINResponse response = HelperController.RegisterProductsService(appkey).GetCompetitivePricingForASIN(request);
                if (response.IsSetGetCompetitivePricingForASINResult())
                {
                    List<GetCompetitivePricingForASINResult> list = response.GetCompetitivePricingForASINResult;
                    List<CompetitiveModel> lsCompetitiveModel = new List<CompetitiveModel>();
                    foreach (var item in list)
                    {
                        CompetitiveModel competitiveModel = new CompetitiveModel();
                        if (item.IsSetProduct())
                        {
                            MarketplaceWebServiceProducts.Model.Product P = item.Product;
                            CompetitivePricingType Ptype = P.CompetitivePricing;

                            int sellerCount = 0;
                            #region sellerCount
                            if (Ptype.NumberOfOfferListings.IsSetOfferListingCount())
                            {
                                OfferListingCountType type = Ptype.NumberOfOfferListings.OfferListingCount.OrderByDescending(s => s.Value).FirstOrDefault();
                                if (type == null)
                                { sellerCount = 1; }
                                else
                                { sellerCount = Convert.ToInt32(type.Value); }
                            }
                            #endregion

                            if (Ptype.CompetitivePrices.IsSetCompetitivePrice())
                            {
                                List<CompetitivePriceType> typePrice = Ptype.CompetitivePrices.CompetitivePrice;
                                List<CompetitivePrice> lsCompetitivePrice = new List<CompetitivePrice>();
                                foreach (var type in typePrice)
                                {
                                    #region CompetitivePrice
                                    CompetitivePrice price = new CompetitivePrice()
                                                           {
                                                               AccountInfoId = appkey.AccountInfoID,
                                                               ASIN = item.Product.Identifiers.MarketplaceASIN.ASIN,
                                                               MarketplaceCode = item.Product.Identifiers.MarketplaceASIN.MarketplaceId,
                                                               belongsToRequester = type.belongsToRequester,
                                                               BtnType = Convert.ToInt16(type.CompetitivePriceId),
                                                               SiteId = null,
                                                               Condition = type.condition,
                                                               CreateTime = DateTime.Now,
                                                               CreateUser = "CompetitivePrice",
                                                               IsDel = false,
                                                               LandedAmount = type.Price.LandedPrice.Amount,
                                                               LandedCurrencyCode = type.Price.LandedPrice.CurrencyCode,
                                                               ListingAmount = type.Price.ListingPrice.Amount,
                                                               ListingCurrencyCode = type.Price.ListingPrice.CurrencyCode,
                                                               ShippingAmount = type.Price.Shipping.Amount,
                                                               ShippingCurrencyCode = type.Price.Shipping.CurrencyCode,
                                                               SubCondition = type.subcondition,
                                                               UpdateTime = DateTime.Now,
                                                               UpdateUser = "UpdateCompetitive",
                                                               SellerCount = sellerCount
                                                           };
                                    #endregion
                                    lsCompetitivePrice.Add(price);
                                }
                                competitiveModel.IsSetCompetitivePriceModelList = lsCompetitivePrice.Count > 0;
                                competitiveModel.CompetitivePriceModelList = lsCompetitivePrice;
                            }

                            if (P.SalesRankings.IsSetSalesRank())
                            {
                                List<SalesRankType> listRank = item.Product.SalesRankings.SalesRank.OrderBy(s => s.Rank).ToList();
                                SalesRankType type = listRank.FirstOrDefault();
                                if (type == null)
                                {
                                    competitiveModel.IsSetSalesRenkModel = false;
                                }
                                else
                                {
                                    #region SalesRank
                                    SalesRank model = new SalesRank()
                                                       {
                                                           AccountInfoId = appkey.AccountInfoID,
                                                           ASIN = item.Product.Identifiers.MarketplaceASIN.ASIN,
                                                           MarketplaceCode = item.Product.Identifiers.MarketplaceASIN.MarketplaceId,
                                                           SiteId = null,
                                                           UpdateTime = DateTime.Now,
                                                           UpdateUser = "SalesRank",
                                                           IsDel = false,
                                                           ProductCategoryId = type.ProductCategoryId,
                                                           RankNumber = Convert.ToInt32(type.Rank),
                                                           SalesRankOtherJson = JsonConvert.SerializeObject(listRank),
                                                           CreateTime = DateTime.Now,
                                                           CreateUser = "UpdaetSalesRank"
                                                       };
                                    #endregion
                                    competitiveModel.IsSetSalesRenkModel = true;
                                    competitiveModel.SalesRenkModel = model;
                                }
                            }
                            lsCompetitiveModel.Add(competitiveModel);
                        }
                    }
                    return lsCompetitiveModel;
                }
                return null;
            }
            catch (Exception)
            {
                return null;
            }
        } 
        #endregion

        #region  2015-11-15 获取最底价信息 +  static void GetLowestOfferListingsForASIN(AppKey appkey, List<string> ASINList)
        /// <summary>
        /// 2015-11-15 获取最底价信息
        /// </summary>
        /// <param name="appkey"></param>
        /// <param name="ASINList"></param>
        public static void GetLowestOfferListingsForASIN(AppKey appkey, List<string> ASINList)
        {
            GetLowestOfferListingsForASINRequest request = new GetLowestOfferListingsForASINRequest()
            {
                ASINList = new ASINListType(),
                MarketplaceId = appkey.MarketplaceCode,
                SellerId = appkey.MerchantCode
            };
            request.ASINList.ASIN = ASINList;
            try
            {
                GetLowestOfferListingsForASINResponse response = HelperController.RegisterProductsService(appkey).GetLowestOfferListingsForASIN(request);
                if (response.IsSetGetLowestOfferListingsForASINResult())
                {
                    List<GetLowestOfferListingsForASINResult> list = response.GetLowestOfferListingsForASINResult;

                    string json = JsonConvert.SerializeObject(response);

                }
            }
            catch (Exception)
            {

                throw;
            }
        } 
        #endregion 
    }
}
